﻿using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;

namespace app.scrum
{

    public class OdbcScrumObject : ScrumObject
    {
        public override int create(ScrumObject sObj)
        {
            int num = 0;
            OdbcConnection con = null;
            OdbcCommand cmd = null;
            try
            {
                con = DatabaseManager.getOdbcConnection();
                cmd = con.CreateCommand();
                if (Util.isEmpty(this.createCmdText))
                {
                    this.genCreateCmdText(sObj);
                }
                cmd.CommandText = this.createCmdText;
                num = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DatabaseManager.closeResource(con, cmd);
            }
            return num;
        }

        public override int update(ScrumObject sObj)
        {
            int num = 0;
            OdbcConnection con = null;
            OdbcCommand cmd = null;
            try
            {
                con = DatabaseManager.getOdbcConnection();
                cmd = con.CreateCommand();
                if (Util.isEmpty(this.updateCmdText))
                {
                    this.genUpdateCmdText(sObj);
                }
                cmd.CommandText = this.updateCmdText;              
                num = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DatabaseManager.closeResource(con, cmd);
            }
            return num;
        }

        public override void genCreateCmdText(ScrumObject sObj)
        {
            this.createCmdText = "insert into [" + this.tableName + "] ( ";
            foreach (ScrumField sField in sObj.fieldList)
            {
                if (String.Equals(sField.name, "objecttype"))
                {
                    continue;
                }
                this.createCmdText += "[" + sField.name + "],";
            }
            this.createCmdText = this.createCmdText.Substring(0, this.createCmdText.Length - 1);
            this.createCmdText += ") values (";
            foreach (ScrumField sField in sObj.fieldList)
            {
                if (String.Equals(sField.name, "objecttype"))
                {
                    continue;
                }
                this.createCmdText += "?,";

            }
            this.createCmdText = this.createCmdText.Substring(0, this.createCmdText.Length - 1);
            this.createCmdText += ")";
        }

        public override void genUpdateCmdText(ScrumObject sObj)
        {
            this.updateCmdText = "update [" + this.tableName + "] set ";
            foreach (ScrumField sField in sObj.fieldList)
            {
                if (String.Equals(sField.name, "objecttype")
                    || String.Equals(sField.name, "id__c"))
                {
                    continue;
                }
                this.updateCmdText += "[" + sField.name + "]=?,";

            }
            this.updateCmdText = this.updateCmdText.Substring(0, this.updateCmdText.Length - 1);
            this.updateCmdText += " where [" + ScrumField.fieldName_Id + "]=" + ScrumObjectHelper.getScrumFieldValue(sObj, ScrumField.fieldName_Id);
        }

        public override void genDeleteCmdText(ScrumObject sObj)
        {
            this.deleteCmdText = "delete from [" + this.tableName + "] "
                   + "where [" + ScrumField.fieldName_Id + "] = " + ScrumObjectHelper.getScrumFieldValue(sObj, ScrumField.fieldName_Id);
        }
    }
}